package com.ruoyi.cms.controller;

import com.ruoyi.common.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cms.domain.CmsChannel;
import com.ruoyi.cms.domain.dto.CmsChannelDTO;
import com.ruoyi.cms.service.ICmsChannelService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.HTExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 栏目信息Controller
 * 
 * @author ruoyi
 * @date 2021-07-30
 */
@Api(tags = "栏目信息")
@Controller
@RequestMapping("/cms/channel")
public class CmsChannelController extends BaseController {

    private final String prefix = "cms/channel";

    @Autowired
    private ICmsChannelService cmsChannelService;

    @RequiresPermissions("cms:channel:view")
    @GetMapping()
    public String channel() {
        return prefix + "/channel";
    }

    /**
     * 查询栏目信息树列表
     */
    @ApiOperation("查询栏目信息树列表")
    @RequiresPermissions("cms:channel:list")
    @PostMapping("/list")
    @ResponseBody
    public List<CmsChannel> list(CmsChannelDTO cmsChannel) {
        List<CmsChannel> list = cmsChannelService.selectCmsChannelList(cmsChannel.as());
        return list;
    }

    /**
     * 导出栏目信息列表
     */
    @ApiOperation(value="导出栏目信息列表", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @RequiresPermissions("cms:channel:export")
    @Log(title = "栏目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult<?> export(CmsChannelDTO cmsChannel) {
        List<CmsChannel> list = cmsChannelService.selectCmsChannelList(cmsChannel.as());
        return HTExcelUtil.exportExcel(CmsChannel.class, list, queryVisibleFields());
    }

    /**
     * 新增栏目信息
     */
    @GetMapping(value = { "/add/{channelId}", "/add/" })
    public String add(@PathVariable(value = "channelId", required = false) Long channelId, ModelMap mmap) {
        if (StringUtils.isNotNull(channelId)) {
            mmap.put("cmsChannel", cmsChannelService.selectCmsChannelById(channelId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存栏目信息
     */
    @ApiOperation("新增保存栏目信息")
    @RequiresPermissions("cms:channel:add")
    @Log(title = "栏目信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult<?> addSave(@Validated CmsChannelDTO cmsChannel) {
        cmsChannel.setChannelId(null);
        cmsChannel.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(cmsChannelService.insertCmsChannel(cmsChannel.as()));
    }

    /**
     * 修改栏目信息
     */
    @GetMapping("/edit/{channelId}")
    public String edit(@PathVariable("channelId") Long channelId, ModelMap mmap)
    {
        CmsChannel cmsChannel = cmsChannelService.selectCmsChannelById(channelId);
        mmap.put("cmsChannel", cmsChannel);
        return prefix + "/edit";
    }

    /**
     * 修改保存栏目信息
     */
    @ApiOperation("修改保存栏目信息")
    @RequiresPermissions("cms:channel:edit")
    @Log(title = "栏目信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult<?> editSave(@Validated CmsChannelDTO cmsChannel) {
        if (null == cmsChannel.getChannelId()) {
            return AjaxResult.error("ID不能为空");
        }
        cmsChannel.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(cmsChannelService.updateCmsChannel(cmsChannel.as()));
    }

    /**
     * 删除栏目信息
     */
    @ApiOperation("删除栏目信息")
    @RequiresPermissions("cms:channel:remove")
    @Log(title = "栏目信息", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{channelId}")
    @ResponseBody
    public AjaxResult<?> remove(@PathVariable("channelId") Long channelId) {
        return toAjax(cmsChannelService.deleteCmsChannelById(channelId));
    }

    /**
     * 选择栏目信息树
     */
    @GetMapping(value = { "/selectChannelTree/{channelId}", "/selectChannelTree/" })
    public String selectChannelTree(@PathVariable(value = "channelId", required = false) Long channelId, ModelMap mmap) {
        if (StringUtils.isNotNull(channelId)) {
            mmap.put("cmsChannel", cmsChannelService.selectCmsChannelById(channelId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载栏目信息树列表
     */
    @ApiOperation("加载栏目信息树列表")
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData() {
        List<Ztree> ztrees = cmsChannelService.selectCmsChannelTree();
        return ztrees;
    }
}
